package com.enjoylearning.checkcode.service.impl;

import com.enjoylearning.checkcode.model.CheckCodeParamsDto;
import com.enjoylearning.checkcode.model.CheckCodeResultDto;
import com.enjoylearning.checkcode.service.CheckCodeService;
import com.enjoylearning.checkcode.service.SendCheckCodeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class SendCheckCodeServiceImpl implements SendCheckCodeService {

    @Resource(name = "NumCheckCodeService")
    private CheckCodeService numCheckCodeService;

    @Override
    public CheckCodeResultDto sendCheckCode(CheckCodeParamsDto checkCodeParamsDto) {
        //获取参数一
        String param1 = checkCodeParamsDto.getParam1();
        //生成验证码
        CheckCodeResultDto checkCodeResultDto = numCheckCodeService.generate(checkCodeParamsDto);
        //定义手机号码的正则表达式
        String regexPhone ="1[3-9]\\d{9}";
        //定义邮箱的正则表达式
        String regexEmail = "\\w+@[\\w&&[^_]]{2,6}(\\.[a-zA-Z]{2,3}){1,2}";
        //根据参数的正则判断发送验证码的方式
        //优先使用手机号验证
        if (param1.matches(regexPhone)) {
            System.out.println("向手机号: "+param1+" 发送验证码: "+checkCodeResultDto.getAliasing());
        }else if (param1.matches(regexEmail)) {
            System.out.println("向邮箱: "+param1+" 发送验证码: "+checkCodeResultDto.getAliasing());
        }else{
            log.debug("手机号或邮箱格式错误，无法解析");
        }
        return checkCodeResultDto;
    }
}
